package pl.uz.zgora.plan;

import android.content.Context;
import android.content.SharedPreferences;

public final class PrefrencesManager {
	private static final String PrefsFilename = "PlanUZ.prefs";
	private static PrefrencesManager mInstance = null;
	private static SharedPreferences mSettings = null;
	private static Context mContext = null;
	
	public void addBooleanToPreferences(final String name, final Boolean value) {
		if(name != null) {
			SharedPreferences.Editor editor = mSettings.edit();
			editor.putBoolean(name, value);
			editor.commit();
		}
	}
	
	public Boolean getBooleanFromPreferences(final String name) {
		if(name != null) {
			 SharedPreferences settings = mContext.getSharedPreferences(PrefsFilename, 0);
		     final Boolean value = settings.getBoolean(name, false);
		     return value;
		}
		return null;
	}
	
	public synchronized void addIntToPreferences(final String name, final Integer value) {
		if(name != null) {
			SharedPreferences.Editor editor = mSettings.edit();
			editor.putInt(name, value);
			editor.commit();
		}
	}
	
	public void addStringToPreferences(final String name, final String value) {
		if(name != null) {
			SharedPreferences.Editor editor = mSettings.edit();
			editor.putString(name, value);
			editor.commit();
		}
	}
	
	public synchronized int getIntFromPreferences(final String name) {
		if(name != null) {
			 SharedPreferences settings = mContext.getSharedPreferences(PrefsFilename, 0);
		     final Integer value = settings.getInt(name, -1);
		     return value;
		}
		return -1;
	}
	
	public String getStringFromPreferences(final String name) {
		if(name != null) {
			 SharedPreferences settings = mContext.getSharedPreferences(PrefsFilename, 0);
		     final String value = settings.getString(name, null);
		     return value;
		}
		return null;
	}
	
	private PrefrencesManager() {};
	
	public static PrefrencesManager getInstance(Context context) {
		if(mInstance == null) {
			mInstance = new PrefrencesManager();
		}
		
		mContext = context;
		mSettings = mContext.getSharedPreferences(PrefsFilename, 0);
		return mInstance;
	}
}
